package com.hongfan.common.utils.encrypt;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;

/**
 * RSA加密算法
 *
 * @author yjr
 */
public class RsaUtils {

    private static final String PRIVATE_KEY = "MIICXQIBAAKBgQCyJJ5Yvq+s0NYF2C2CamUq38R3oL8qUo62B5anmftGHfuT/kQrRapeGfQKwNAiA146mSdmVmrLieDF4i+Hya5n1mrZr7Lch5HMCQ1fwJ5uo43c+1lEXJ1oMJcQa8RtJnC2Tef/EzFyxBw5nFLuzrstyNZelKW6bAe9pVZvMGUjlwIDAQABAoGAU3YNi/V21+gVxbI1DCttz4r8ALIarij0vLrMbiSZ0Y19jyYq6P0gJytQp3MTcEQUsy6LA4rMSSUFXqCsdRlb8HjSqtDuW3qCA0xUuyen28+szRrDQqLu3/GlLRTLaKdKMSDlY/cHo30xF3B1/Kx89C05Pns6Iat5KVTu/OrM72ECQQDJNqGap+fTCD+49H0ePrxpKqKov1X4YgfESZAwEsUmhf1Wc2+ZN4KFlnFsjcnv6QDEbjBHj+lx2JPI8pAVBry3AkEA4qXkVb++YhMugR0FVPa8NTRzrzUdUVjp1XnDVX2K+q2TYEdkmwJANgNxvQZ645W4DoVjxR6uE1G0Ek5rln+wIQJAffvQz47V3hJKI7ohJPDO82IYmmPCplRq3IHa397AxlAawShh9aMF/DKoLfnROH/BVTxQyl3XIssiLS2C/UdMnQJBAIgCRSOwDO+N0zpnz+tMpWTINSPPG+jVEn2otC95Rw1nXClKrnTWxPGk33wvh09LaUhNg7Gm14+AWC8kICwQNAECQQCqlJbFllX/izix1XK0558fhaSFCQSVbM5lvJH5YlLSyQmRvRFHM94EYG1Rp1reZ+el5jVNFyLV5byvrt/cPUcE";

    private static final String PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCyJJ5Yvq+s0NYF2C2CamUq38R3oL8qUo62B5anmftGHfuT/kQrRapeGfQKwNAiA146mSdmVmrLieDF4i+Hya5n1mrZr7Lch5HMCQ1fwJ5uo43c+1lEXJ1oMJcQa8RtJnC2Tef/EzFyxBw5nFLuzrstyNZelKW6bAe9pVZvMGUjlwIDAQAB";

    /**
     * RSA加密
     *
     * @param content 要加密的内容
     * @return 加密结果
     */
    public static String encrypt(String content) {
        RSA rsa = new RSA(PRIVATE_KEY, PUBLIC_KEY);
        byte[] aByte = rsa.encrypt(StrUtil.bytes(content, CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
        return Base64.encode(aByte);
    }

    /**
     * RSA解密
     *
     * @param content 要解密的内容
     * @return 解密结果
     */
    public static String decrypt(String content) {
        RSA rsa = new RSA(PRIVATE_KEY, PUBLIC_KEY);
        byte[] aByte = rsa.decrypt(content, KeyType.PrivateKey);
        return StrUtil.str(aByte, CharsetUtil.CHARSET_UTF_8);
    }
}
